package com.cwits.wifi.ui.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cwits.wifi.MainApplication;
import com.cwits.wifi.R;
import com.cwits.wifi.base.BaseActivity;
import com.cwits.wifi.manager.ControlManager;
import com.cwits.wifi.ui.dialog.ExpouseDialog;
import com.cwits.wifi.ui.dialog.ModifyDeviceDialog;
import com.cwits.wifi.ui.dialog.ModifyDeviceNameDialog;
import com.cwits.wifi.ui.dialog.ModifyDevicePasswordDialog;
import com.cwits.wifi.ui.dialog.TwoDialog;
import com.cwits.wifi.ui.service.CommunicationService;
import com.cwits.wifi.util.Dbug;
import com.cwits.wifi.util.IAction;
import com.cwits.wifi.util.PreferencesHelper;
import com.jieli.lib.stream.beans.StateInfo;
import com.jieli.lib.stream.tools.CommandHub;
import com.jieli.lib.stream.util.ICommon;

/**
 * Created by ZhangHua on 16-03-01.
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener, ICommon {

    private boolean isFirst = false;
    private boolean isUpdataSelect = false;
    private TwoDialog twoDialog;
    private boolean CHANGE_PWD = false;
    private boolean CHANGE_SSID = false;
    private boolean SUCESS_CHANGE_SSID = false;
    private boolean SUCESS_CHANGE_PWD = false;
    private RelativeLayout mBack;
    private TextView mTvName, mTvPwd;
    private Button mBtnName, mBtnPwd;
    private ProgressDialog pd, pb1, pdFormat;
    private ModifyDeviceDialog modifyDialog;
    private ModifyDeviceNameDialog modifyWifiDialog;
    private ModifyDevicePasswordDialog modifyDevicePasswordDialog;
    private TextView mTv7202, mTvVga2;
    private LinearLayout mLl720, mLlVga;
    private RelativeLayout mRlWaring;
    private ImageView mImSound, mImShock;
    private boolean isSound = true;   //录音
    private boolean isShock = true;   //停车守卫
    private boolean isBeep = true;    //按键
    private boolean isPlay = true;    //运动检测
    private int resolution = 1;
    private String mOldName, mOldPassword;
    private boolean isOnLine = false;
    private static String mVideoSize, mRecode, mShock, mLoop, mPlay, mBeep, mClose, mScreen, mHz, mExposure, mLanguage, mHaver, mWhite;
    private boolean BACK_RECODE = false;
    private boolean BACK_SHOCK = false;
    private boolean BACK_RESOLUTION = false;
    private boolean BACK_LOOP = true;
    private boolean BACK_CLOSE = false;
    private boolean BACK_PLAY = false;
    private boolean BACK_BEEP = false;
    private boolean BACK_SCREEN = false;
    private boolean BACK_HZ = false;
    private boolean BACK_EXPOSURE = false;
    private boolean BACK_Language = false;
    private boolean BACK_HAVER = false;
    private boolean BACK_WHITE = false;


    private boolean VIDEO_OPEN = false;

    private RelativeLayout mRlFormat;   //格式化
    private RelativeLayout mRlDefault;   //默认设置


    private RelativeLayout mRlExposure;   //曝光补偿

    private TextView mTvExposure;
    private RelativeLayout mRlLoopTime;    //分段录影时长
    private TextView mTvLoopTime;
    private RelativeLayout mRlClose;         //自动关机
    private TextView mTvClose;
    private RelativeLayout mRlScreensave;    //屏幕保护
    private TextView mTvScreensave;
    private RelativeLayout mRlwhite;          //白平衡
    private TextView mTvWhite;
    private RelativeLayout mRlNormall;        //重力感应
    private TextView mTvNormall;
    private RelativeLayout mRlHz;            //光源频率
    private TextView mTvHz;
    private RelativeLayout mRlLanguage;            //语言设置
    private TextView mTvLanguage;
    private TextView mTvChange;
    private ImageView mImPlay;                 //运动检测
    private ImageView mImBeep;                 //按键声音

    private ExpouseDialog exWindow;

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ACTION_SPECIAL_DATA)) {
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_SPECIAL_STATE);

                String param1 = stateInfo.getParam()[0];

                String cmdResult = stateInfo.getCmdNumber();
                if (cmdResult.equals(CMD_START_RECORD))
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_STOP_RECORD, ARGS_STOP_RECORD);
            } else if (action.equals(ACTION_GET_VIDEO_SIZE_STATUS)) {
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_VIDEO_SIZE_STATUS);
                String param = stateInfo.getParam()[0];
                mVideoSize = param;
                resolution = Integer.valueOf(mVideoSize);
                handler1.sendEmptyMessage(1113);
            } else if (action.equals(ACTION_GET_RECORDING_VOICE_STATUS)) {
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_RECORDING_VOICE_STATUS);
                String param = stateInfo.getParam()[0];
                mRecode = param;
                handler1.sendEmptyMessage(1116);
            } else if (action.equals(ACTION_GET_SOS_SHAKE_STATUS)) {
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_SOS_SHAKE_STATUS);
                String param = stateInfo.getParam()[0];
                mShock = param;
                handler1.sendEmptyMessage(1117);

            } else if (action.equals(ACTION_GET_LOOPRECORD_STATUS)) {
                //分段录影时长
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_LOOPRECORD__STATUS);
                String param = stateInfo.getParam()[0];
                mLoop = param;
                handler1.sendEmptyMessage(2002);
            } else if (action.equals(ACTION_GET_DETECTDV_STATUS)) {
                //运动检测
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_DETECTDV__STATUS);
                String param = stateInfo.getParam()[0];
                mPlay = param;
                handler1.sendEmptyMessage(3010);
            } else if (action.equals(ACTION_GET_DEEPVOICE_STATUS)) {
                //按键声音
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_DEEPVOICE__STATUS);
                String param = stateInfo.getParam()[0];
                mBeep = param;
                handler1.sendEmptyMessage(3100);
            } else if (action.equals(ACTION_GET_AUTOCLOSE_STATUS)) {
                //自动关机
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_AUTOCLOSE__STATUS);
                String param = stateInfo.getParam()[0];
                mClose = param;
                handler1.sendEmptyMessage(3001);
            } else if (action.equals(ACTION_GET_SCREENSAVE_STATUS)) {
                //屏幕保护
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_SCREENSAVE__STATUS);
                String param = stateInfo.getParam()[0];
                mScreen = param;
                handler1.sendEmptyMessage(3002);
            } else if (action.equals(ACTION_GET_SUNNY_STATUS)) {
                //hz
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_SUNNY__STATUS);
                String param = stateInfo.getParam()[0];
                mHz = param;
                handler1.sendEmptyMessage(3008);
            } else if (action.equals(ACTION_GET_ADD_SUNNY_STATUS)) {
                //曝光补偿
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_ADD_SUNNY__STATUS);
                String param = stateInfo.getParam()[0];
                mExposure = param;
                handler1.sendEmptyMessage(3000);
            } else if (action.equals(ACTION_GET_LANGUNE_STATUS)) {
                //语言
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_LANGUNE__STATUS);
                String param = stateInfo.getParam()[0];
                mLanguage = param;
                handler1.sendEmptyMessage(3006);
            } else if (action.equals(ACTION_GET_HEAVRY_STATUS)) {
                //重力
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_HEAVRY__STATUS);
                String param = stateInfo.getParam()[0];
                mHaver = param;
                handler1.sendEmptyMessage(2009);
            } else if (action.equals(ACTION_GET_WHITE_BALANCE_STATUS)) {
                //白平衡
                final StateInfo stateInfo = (StateInfo) intent.getSerializableExtra(KEY_GET_WHITE_BALANCE__STATUS);
                String param = stateInfo.getParam()[0];
                mWhite = param;
                handler1.sendEmptyMessage(0022);

            } else if (action.equals(ACTION_FORMAT_SDCARD)) {
                //格式化成功
                if (null != pdFormat)
                    pdFormat.dismiss();
                showToastLong(getString(R.string.format_success));
                MainActivity.CLEAN_ALL = true;

            } else if (action.equals(ACTION_RESET_DEVICE)) {
                //恢复默认成功
                isFirst = true;
                showToastLong(getString(R.string.reset_success));
                ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_RESTART_DEVICE, ARGS_RESTART_DEVICE);
                changeWiFi();
                mApplication.setIsOffLineMode(true);
                mApplication.setRealCamera(false);
            } else if (action.equals(ACTION_CHANGE_PWD_SUCCESS)) {
                //密码修改成功
                SUCESS_CHANGE_PWD = true;
                handler1.sendEmptyMessage(1);
            } else if (action.equals(ACTION_CHANGE_SSID_SUCCESS)) {
                //设备名称修改成功
                SUCESS_CHANGE_SSID = true;
                handler1.sendEmptyMessage(1);
            } else if (action.equals(ACTION_CHANGE_PWD_FAILED)) {
                //密码修改失败
                SUCESS_CHANGE_PWD = false;
                showToastLong(getString(R.string.pwd_change_err));
            } else if (action.equals(ACTION_CHANGE_SSID_FAILED)) {
                //设备名称修改失败
                SUCESS_CHANGE_SSID = false;
                showToastLong(getString(R.string.name_change_err));
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_infolayout);
        isFirst = true;
        init();

        IntentFilter intentFilter = new IntentFilter(IAction.ACTION_SPECIAL_DATA);
        intentFilter.addAction(ACTION_DEVICE_CONNECTION_SUCCESS);
        intentFilter.addAction(ACTION_GET_RECORDING_VOICE_STATUS);
        intentFilter.addAction(ACTION_GET_SOS_SHAKE_STATUS);
        intentFilter.addAction(ACTION_GET_VIDEO_SIZE_STATUS);
        intentFilter.addAction(ACTION_GET_LOOPRECORD_STATUS);
        intentFilter.addAction(ACTION_GET_DETECTDV_STATUS);
        intentFilter.addAction(ACTION_GET_DEEPVOICE_STATUS);
        intentFilter.addAction(ACTION_GET_AUTOCLOSE_STATUS);
        intentFilter.addAction(ACTION_GET_SCREENSAVE_STATUS);
        intentFilter.addAction(ACTION_GET_SUNNY_STATUS);
        intentFilter.addAction(ACTION_GET_ADD_SUNNY_STATUS);
        intentFilter.addAction(ACTION_GET_LANGUNE_STATUS);
        intentFilter.addAction(ACTION_GET_HEAVRY_STATUS);
        intentFilter.addAction(ACTION_FORMAT_SDCARD);
        intentFilter.addAction(ACTION_RESET_DEVICE);
        intentFilter.addAction(ACTION_GET_WHITE_BALANCE_STATUS);
        intentFilter.addAction(ACTION_CHANGE_PWD_SUCCESS);
        intentFilter.addAction(ACTION_CHANGE_SSID_SUCCESS);
        intentFilter.addAction(ACTION_CHANGE_PWD_FAILED);
        intentFilter.addAction(ACTION_CHANGE_SSID_FAILED);

        registerReceiver(mReceiver, intentFilter);

        if (mApplication.getIsOffLineMode() == false) {
            //已经连接设备wifi
            //showProcessDialog();
            ControlManager.getInstance().requestStatus("1", "50");

            mRlWaring.setVisibility(View.GONE);
            pb1 = ProgressDialog.show(SettingActivity.this, getString(R.string.dialog_tip), getString(R.string.loading_wait));
            initSetting();
        } else {
            //没有连接设备wifi
            //showToast("对不起，没有连接168设备WiFi！");
            mRlWaring.setVisibility(View.VISIBLE);
        }

    }

    private void initSetting() {

        sendMessage();

        String recordState = CommunicationService.getInstance().getDeviceStatus(CMD_GET_RECORDING_STATUS);
        if (ARGS_IN_RECORDING.equals(recordState)) {
            VIDEO_OPEN = true;
        }
    }


    private void init() {
        mRlWaring = (RelativeLayout) findViewById(R.id.setting_rl_warring);
        mBack = (RelativeLayout) findViewById(R.id.left_imageview);
        mTvName = (TextView) findViewById(R.id.setting_tv_name);
        mTvPwd = (TextView) findViewById(R.id.setting_tv_pwd);
        mBtnName = (Button) findViewById(R.id.setting_btn_name);
        mBtnPwd = (Button) findViewById(R.id.setting_btn_pwd);
        mTv7202 = (TextView) findViewById(R.id.setting_tv_7202);
        mTvVga2 = (TextView) findViewById(R.id.setting_tv_vga2);
        mLl720 = (LinearLayout) findViewById(R.id.setting_ll_720);
        mLlVga = (LinearLayout) findViewById(R.id.setting_ll_vga);
        mLl720.setOnClickListener(this);
        mLlVga.setOnClickListener(this);
        mTvChange = (TextView) findViewById(R.id.setting_tv_change);
        mTvChange.setOnClickListener(this);
        //按键声音
        mImBeep = (ImageView) findViewById(R.id.setting_im_beep);
        mImBeep.setOnClickListener(this);
        //运动检测
        mImPlay = (ImageView) findViewById(R.id.setting_im_play_detect);
        mImPlay.setOnClickListener(this);

        //格式2

        //分段录影时长
        mRlLoopTime = (RelativeLayout) findViewById(R.id.setting_rl_loop_time);
        mTvLoopTime = (TextView) findViewById(R.id.setting_tv_loop_recording_time);
        mRlLoopTime.setOnClickListener(this);

        //自动关机
        mRlClose = (RelativeLayout) findViewById(R.id.setting_rl_close);
        mTvClose = (TextView) findViewById(R.id.setting_tv_close);
        mRlClose.setOnClickListener(this);

        //屏幕保护
        mRlScreensave = (RelativeLayout) findViewById(R.id.setting_rl_screensave);
        mTvScreensave = (TextView) findViewById(R.id.setting_tv_screensave);
        mRlScreensave.setOnClickListener(this);

        //白平衡
        mRlwhite = (RelativeLayout) findViewById(R.id.setting_rl_white);
        mTvWhite = (TextView) findViewById(R.id.setting_tv_white);
        mRlwhite.setOnClickListener(this);

        //重力感应
        mRlNormall = (RelativeLayout) findViewById(R.id.setting_rl_normall);
        mTvNormall = (TextView) findViewById(R.id.setting_tv_normall);
        mRlNormall.setOnClickListener(this);

        //光源频率
        mRlHz = (RelativeLayout) findViewById(R.id.setting_rl_hz);
        mTvHz = (TextView) findViewById(R.id.setting_tv_hz);
        mRlHz.setOnClickListener(this);

        //曝光补偿
        mRlExposure = (RelativeLayout) findViewById(R.id.setting_rl_exposure);
        mTvExposure = (TextView) findViewById(R.id.setting_tv_exposure);
        mRlExposure.setOnClickListener(this);

        //语言设置
        mRlLanguage = (RelativeLayout) findViewById(R.id.setting_rl_language);
        mTvLanguage = (TextView) findViewById(R.id.setting_tv_language);
        mRlLanguage.setOnClickListener(this);

        //格式3
        mRlFormat = (RelativeLayout) findViewById(R.id.setting_rl_format);
        mRlDefault = (RelativeLayout) findViewById(R.id.setting_rl_default);
        mRlFormat.setOnClickListener(this);
        mRlDefault.setOnClickListener(this);

        mImSound = (ImageView) findViewById(R.id.setting_im_sound);
        mImShock = (ImageView) findViewById(R.id.setting_im_shock);
        mImSound.setOnClickListener(this);
        mImShock.setOnClickListener(this);
        mBtnName.setOnClickListener(this);
        mBtnPwd.setOnClickListener(this);
        mBack.setOnClickListener(this);
    }

    public void changeWiFi() {
        pd = ProgressDialog.show(SettingActivity.this, getString(R.string.dialog_tip), getString(R.string.close_wait));
        handler1.sendEmptyMessageDelayed(1111, 15000);
    }

    @Override
    protected void onResume() {
        super.onResume();
        upData();
        //handler1.sendEmptyMessage(1114);

    }

    /**
     * 分段录影时长
     */
    private View.OnClickListener looptimeOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            exWindow.dismiss();
            switch (v.getId()) {
                case R.id.dialog_expouse__2:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "2002", "1");
                    mTvLoopTime.setText(getString(R.string.min_1));
                    break;
                case R.id.dialog_expouse__1:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "2002", "2");
                    mTvLoopTime.setText(getString(R.string.min_2));
                    break;
                case R.id.dialog_expouse_0:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "2002", "3");
                    mTvLoopTime.setText(getString(R.string.min_3));
                    break;
                default:
                    break;
            }


        }

    };
    /**
     * 自动关机
     */
    private View.OnClickListener closeOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            exWindow.dismiss();
            switch (v.getId()) {
                case R.id.dialog_expouse__3:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3001", "0");
                    mTvClose.setText(getString(R.string.setting_s0));
                    break;
                case R.id.dialog_expouse__2:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3001", "1");
                    mTvClose.setText(getString(R.string.setting_s1));
                    break;
                case R.id.dialog_expouse__1:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3001", "2");
                    mTvClose.setText(getString(R.string.setting_s2));
                    break;
                case R.id.dialog_expouse_0:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3001", "3");
                    mTvClose.setText(getString(R.string.setting_s3));
                    break;
                default:
                    break;
            }


        }

    };
    /**
     * 屏幕保护
     */
    private View.OnClickListener screensaveOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            exWindow.dismiss();
            switch (v.getId()) {
                case R.id.dialog_expouse__3:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3002", "0");
                    mTvScreensave.setText(getString(R.string.setting_s0));
                    break;
                case R.id.dialog_expouse__2:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3002", "1");
                    mTvScreensave.setText(getString(R.string.setting_s3));
                    break;
                case R.id.dialog_expouse__1:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3002", "2");
                    mTvScreensave.setText(getString(R.string.min_1));
                    break;
                case R.id.dialog_expouse_0:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3002", "3");
                    mTvScreensave.setText(getString(R.string.min_2));
                    break;
                default:
                    break;
            }
        }

    };
    /**
     * 白平衡
     */
    private View.OnClickListener whiteOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            exWindow.dismiss();
            switch (v.getId()) {
                case R.id.dialog_expouse__3:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_WHITE_BALANCE, "0");
                    mTvWhite.setText(getString(R.string.white_1));
                    break;
                case R.id.dialog_expouse__2:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_WHITE_BALANCE, "1");
                    mTvWhite.setText(getString(R.string.white_2));
                    break;
                case R.id.dialog_expouse__1:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_WHITE_BALANCE, "2");
                    mTvWhite.setText(getString(R.string.white_3));
                    break;
                case R.id.dialog_expouse_0:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_WHITE_BALANCE, "3");
                    mTvWhite.setText(getString(R.string.white_4));
                    break;
                case R.id.dialog_expouse_1:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_WHITE_BALANCE, "4");
                    mTvWhite.setText(getString(R.string.white_5));
                    break;
                default:
                    break;
            }
        }

    };
    /**
     * 重力感应
     */
    private View.OnClickListener normallOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            exWindow.dismiss();
            switch (v.getId()) {
                case R.id.dialog_expouse__3:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "2009", "0");
                    mTvNormall.setText(getString(R.string.setting_s0));
                    break;
                case R.id.dialog_expouse__2:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "2009", "1");
                    mTvNormall.setText(getString(R.string.normall_1));
                    break;
                case R.id.dialog_expouse__1:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "2009", "2");
                    mTvNormall.setText(getString(R.string.normall_2));
                    break;
                case R.id.dialog_expouse_0:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "2009", "3");
                    mTvNormall.setText(getString(R.string.normall_3));
                    break;
                default:
                    break;
            }
        }

    };
    /**
     * 光源频率
     */
    private View.OnClickListener hzOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            exWindow.dismiss();
            switch (v.getId()) {
                case R.id.dialog_expouse__3:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3008", "0");
                    mTvHz.setText("50Hz");
                    break;
                case R.id.dialog_expouse__2:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3008", "1");
                    mTvHz.setText("60Hz");
                    break;
                default:
                    break;
            }
        }

    };
    /**
     * 曝光补偿
     */
    private View.OnClickListener expouseOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            exWindow.dismiss();
            switch (v.getId()) {
                case R.id.dialog_expouse__3:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3000", "0");
                    mTvExposure.setText("-3");
                    break;
                case R.id.dialog_expouse__2:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3000", "1");
                    mTvExposure.setText("-2");
                    break;
                case R.id.dialog_expouse__1:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3000", "2");
                    mTvExposure.setText("-1");
                    break;
                case R.id.dialog_expouse_0:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3000", "3");
                    mTvExposure.setText("0");
                    break;
                case R.id.dialog_expouse_1:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3000", "4");
                    mTvExposure.setText("1");
                    break;
                case R.id.dialog_expouse_2:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3000", "5");
                    mTvExposure.setText("2");
                    break;
                case R.id.dialog_expouse_3:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3000", "6");
                    mTvExposure.setText("3");
                    break;
                default:
                    break;
            }
        }

    };
    /**
     * 语言设置
     */
    private View.OnClickListener languageOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            exWindow.dismiss();
            switch (v.getId()) {
                case R.id.dialog_expouse__3:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3006", "0");
                    mTvLanguage.setText(getString(R.string.language_cha));
                    break;
                case R.id.dialog_expouse__2:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3006", "1");
                    mTvLanguage.setText(getString(R.string.language_chi));
                    break;
                case R.id.dialog_expouse__1:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3006", "2");
                    mTvLanguage.setText(getString(R.string.language_eng));
                    break;
                case R.id.dialog_expouse_0:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3006", "3");
                    mTvLanguage.setText(getString(R.string.language_deu));
                    break;
                case R.id.dialog_expouse_1:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3006", "4");
                    mTvLanguage.setText(getString(R.string.language_jap));
                    break;

                default:
                    break;
            }
        }

    };

    private void onChangeNamePwd() {
        SUCESS_CHANGE_PWD = false;
        SUCESS_CHANGE_SSID = false;
        CHANGE_SSID = false;
        CHANGE_PWD = false;

        modifyDialog = ModifyDeviceDialog.newInstance();
        modifyDialog.setOnEndListener(mOnChangeListener);
        modifyDialog.show(getFragmentManager(), "ModifyDeviceDialog");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_imageview:
                finish();
                break;
            /** 修改名称和密码 */
            case R.id.setting_tv_change:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    onChangeNamePwd();

                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }

                break;
            /** 运动检测 */
            case R.id.setting_im_play_detect:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    if (isPlay) {
                        isPlay = false;
                        mImPlay.setImageResource(R.mipmap.switch_off);
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3010", "0");
                        //CommandHub.getInstance().requestStatus();
                    } else {
                        isPlay = true;
                        mImPlay.setImageResource(R.mipmap.switch_on);
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3010", "1");
                    }
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;
            /** 按键声音 */
            case R.id.setting_im_beep:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    if (isBeep) {
                        isBeep = false;
                        mImBeep.setImageResource(R.mipmap.switch_off);
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3100", "0");
                    } else {
                        isBeep = true;
                        mImBeep.setImageResource(R.mipmap.switch_on);
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3100", "1");
                    }

                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;
            /** 分段录影时长 */
            case R.id.setting_rl_loop_time:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    //int type = Integer.valueOf(mLoop);
                    exWindow = new ExpouseDialog(SettingActivity.this, looptimeOnClick, mRlExposure, 1, Integer.valueOf(mLoop));
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;
            /** 自动关机*/
            case R.id.setting_rl_close:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    exWindow = new ExpouseDialog(SettingActivity.this, closeOnClick, mRlExposure, 2, Integer.valueOf(mClose));
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;
            /** 屏幕保护 */
            case R.id.setting_rl_screensave:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    exWindow = new ExpouseDialog(SettingActivity.this, screensaveOnClick, mRlExposure, 3, Integer.valueOf(mScreen));
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;
            /** 白平衡 */
            case R.id.setting_rl_white:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    exWindow = new ExpouseDialog(SettingActivity.this, whiteOnClick, mRlExposure, 4, Integer.valueOf(mWhite));
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;
            /** 重力感应 */
            case R.id.setting_rl_normall:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    exWindow = new ExpouseDialog(SettingActivity.this, normallOnClick, mRlExposure, 5, Integer.valueOf(mHaver));
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;
            /** 光源频率 */
            case R.id.setting_rl_hz:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    exWindow = new ExpouseDialog(SettingActivity.this, hzOnClick, mRlExposure, 6, Integer.valueOf(mHz));
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;

            /** 曝光补偿 */
            case R.id.setting_rl_exposure:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    exWindow = new ExpouseDialog(SettingActivity.this, expouseOnClick, mRlExposure, 7, Integer.valueOf(mExposure));
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }

                break;
            /** 语言设置 */
            case R.id.setting_rl_language:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    exWindow = new ExpouseDialog(SettingActivity.this, languageOnClick, mRlExposure, 8, Integer.valueOf(mLanguage));
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;

            /** 恢复默认设置 */
            case R.id.setting_rl_default:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    //showThreeDialog(true);
                    //CommandHub.getInstance().requestStatus(CTP_ID_DEFAULT, "0600");
                    CommandHub.getInstance().requestStatus("2", "0600");
                    Dbug.i("SettingActivity", "-------" + "已发送命令：" + "0600");

                    showDefault();
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }

                break;
            /** 格式化 */
            case R.id.setting_rl_format:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    // showThreeDialog(false);
                    showFormat();
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }

                break;
            case R.id.setting_ll_vga:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    if (resolution != 1) {
                        resolution = 1;
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_VIDEO_SIZE, "1");
                        mTvVga2.setTextColor(getResources().getColor(R.color.text_white));
                        mLlVga.setBackgroundResource(R.mipmap.btn_select_sense);
                        mTv7202.setTextColor(getResources().getColor(R.color.setting_normal2));
                        mLl720.setBackgroundResource(R.mipmap.btn_no_select_sense);
                    }
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;
            case R.id.setting_ll_720:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    if (resolution != 0) {
                        resolution = 0;
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_VIDEO_SIZE, "0");
                        mTv7202.setTextColor(getResources().getColor(R.color.text_white));
                        mLl720.setBackgroundResource(R.mipmap.btn_select_sense);
                        mTvVga2.setTextColor(getResources().getColor(R.color.setting_normal2));
                        mLlVga.setBackgroundResource(R.mipmap.btn_no_select_sense);
                    }
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }

                break;
            case R.id.setting_btn_name:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    modifyWifiDialog = ModifyDeviceNameDialog.newInstance();
                    modifyWifiDialog.setOnEndListener(mOnEndListener);
                    modifyWifiDialog.show(getFragmentManager(), "ModifyDeviceNameDialog");
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }

                break;
            case R.id.setting_btn_pwd:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    modifyDevicePasswordDialog = ModifyDevicePasswordDialog.newInstance();
                    modifyDevicePasswordDialog.setOnEndListener(mOnEndPwdListener);
                    modifyDevicePasswordDialog.show(getFragmentManager(), "ModifyDevicePasswordDialog");
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }
                break;
            case R.id.setting_im_sound:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi
                    if (isSound) {
                        mImSound.setImageResource(R.mipmap.switch_off);
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3101", "0");
                        isSound = false;
                    } else {
                        mImSound.setImageResource(R.mipmap.switch_on);
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "3101", "1");
                        isSound = true;
                    }
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }

                break;
            case R.id.setting_im_shock:
                if (mApplication.getIsOffLineMode() == false) {
                    //已经连接设备wifi

                    if (isShock) {
                        isShock = false;
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "2008", "0");
                        mImShock.setImageResource(R.mipmap.switch_off);
                    } else {
                        isShock = true;
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "2008", "1");
                        mImShock.setImageResource(R.mipmap.switch_on);
                    }
                } else {
                    //没有连接设备wifi
                    showToast(getString(R.string.please_connect));
                }

                break;
        }
    }

    final ModifyDeviceNameDialog.OnEndListener mOnEndListener = new ModifyDeviceNameDialog.OnEndListener() {
        @Override
        public void OnEnd(boolean is_reset) {
            if (is_reset == true) {
                changeWiFi();
                mApplication.setIsOffLineMode(true);
                mApplication.setRealCamera(false);

            }
        }

    };

    final ModifyDevicePasswordDialog.mOnEndPwdListener mOnEndPwdListener = new ModifyDevicePasswordDialog.mOnEndPwdListener() {
        @Override
        public void OnEnd(boolean is_reset) {
            if (is_reset == true) {
                changeWiFi();
                mApplication.setIsOffLineMode(true);
                mApplication.setRealCamera(false);

            }
        }
    };

    final ModifyDeviceDialog.OnChangeListener mOnChangeListener = new ModifyDeviceDialog.OnChangeListener() {


        @Override
        public void ChangePwd(boolean is_pwd) {
            isFirst = true;
            CHANGE_PWD = is_pwd;
        }

        @Override
        public void ChangeSsid(boolean is_ssid) {
            isFirst = true;
            CHANGE_SSID = is_ssid;
        }
    };

    @Override
    protected void onStart() {
        super.onStart();


    }

    @Override
    protected void onStop() {
        super.onStop();


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
        if (!mApplication.getIsOffLineMode()) {
            closeVideo();
        }
    }

    private void closeVideo() {
        if (VIDEO_OPEN) {
            String recordState = CommunicationService.getInstance().getDeviceStatus(CMD_GET_RECORDING_STATUS);
            if (!recordState.equals(ARGS_IN_RECORDING)) {
                ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_START_RECORD, ARGS_START_RECORD);
            }
        }

    }

    private Handler handler1 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    if (CHANGE_PWD == SUCESS_CHANGE_PWD && CHANGE_SSID == SUCESS_CHANGE_SSID) {
                        //修改完成，发送重启
                        isUpdataSelect = true;
                        ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_RESTART_DEVICE, ARGS_RESTART_DEVICE);
                        changeWiFi();
                        mApplication.setIsOffLineMode(true);
                        mApplication.setRealCamera(false);
                        showToastLong(getString(R.string.sucess_mod));
                    }
                    break;
                case 9999:
                    if (isFirst) {
                        isFirst = false;
                        String recordState = CommunicationService.getInstance().getDeviceStatus(CMD_GET_RECORDING_STATUS);
                        if (ARGS_IN_RECORDING.equals(recordState)) {
                            ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_STOP_RECORD, ARGS_STOP_RECORD);
                        }
                    }

                    if (pb1 != null)
                        pb1.dismiss();
                    break;
                case 1111:
                    if (pd != null)
                        pd.dismiss();
                    disconnectDevice();
                    searchWiFiBySetting();
                    break;
                case 1112:
                    mTvName.setText(getString(R.string.err_mod));
                    mTvPwd.setText(getString(R.string.err_mod));
                    break;
                case 1113:
                    if ("1".equals(mVideoSize)) {
                        mTvVga2.setTextColor(getResources().getColor(R.color.text_white));
                        mLlVga.setBackgroundResource(R.mipmap.btn_select_sense);
                        mTv7202.setTextColor(getResources().getColor(R.color.setting_normal2));
                        mLl720.setBackgroundResource(R.mipmap.btn_no_select_sense);
                    } else if ("0".equals(mVideoSize)) {
                        mTv7202.setTextColor(getResources().getColor(R.color.text_white));
                        mLl720.setBackgroundResource(R.mipmap.btn_select_sense);
                        mTvVga2.setTextColor(getResources().getColor(R.color.setting_normal2));
                        mLlVga.setBackgroundResource(R.mipmap.btn_no_select_sense);
                    }
                    BACK_RESOLUTION = true;
                    handler1.sendEmptyMessage(1115);

                    break;
                case 1114:
                    pb1 = ProgressDialog.show(SettingActivity.this, getString(R.string.dialog_tip), getString(R.string.loading_wait));
                    break;
                case 1115:   /** 都来这里  */
                    if (BACK_WHITE && BACK_HAVER && BACK_Language && BACK_EXPOSURE && BACK_HZ && BACK_RECODE && BACK_RESOLUTION && BACK_SHOCK && BACK_LOOP && BACK_PLAY && BACK_BEEP && BACK_CLOSE && BACK_SCREEN) {


                        handler1.removeMessages(9999);
                        handler1.sendEmptyMessageDelayed(9999, 4000);

                    }
                    break;
                case 1116:
                    if ("0".equals(mRecode)) {
                        //录音关
                        mImSound.setImageResource(R.mipmap.switch_off);
                        isSound = false;
                    } else if ("1".equals(mRecode)) {
                        //录音开
                        isSound = true;
                        mImSound.setImageResource(R.mipmap.switch_on);
                    }
                    BACK_RECODE = true;
                    handler1.sendEmptyMessage(1115);
                    break;
                case 1117:
                    if ("0".equals(mShock)) {
                        //震动关
                        mImShock.setImageResource(R.mipmap.switch_off);
                        isShock = false;
                    } else if ("1".equals(mShock) || "2".equals(mShock) || "3".equals(mShock)) {
                        //震动开
                        isShock = true;
                        mImShock.setImageResource(R.mipmap.switch_on);
                    }
                    BACK_SHOCK = true;
                    handler1.sendEmptyMessage(1115);
                    break;
                /** 分段录影 */
                case 2002:

                    int type = Integer.valueOf(mLoop);
                    switch (type) {
                        case 1:
                            mTvLoopTime.setText(getString(R.string.min_1));
                            break;
                        case 2:
                            mTvLoopTime.setText(getString(R.string.min_2));
                            break;
                        case 3:
                            mTvLoopTime.setText(getString(R.string.min_3));
                            break;
                    }
                    BACK_LOOP = true;
                    handler1.sendEmptyMessage(1115);
                    break;
                /** 运动检测 */
                case 3010:
                    if ("0".equals(mPlay)) {
                        mImPlay.setImageResource(R.mipmap.switch_off);
                        isPlay = false;
                    } else if ("1".equals(mPlay)) {
                        isPlay = true;
                        mImPlay.setImageResource(R.mipmap.switch_on);
                    }
                    BACK_PLAY = true;
                    handler1.sendEmptyMessage(1115);
                    break;
                /** 按键声音 */
                case 3100:
                    if ("0".equals(mBeep)) {
                        mImBeep.setImageResource(R.mipmap.switch_off);
                        isBeep = false;
                    } else if ("1".equals(mBeep)) {
                        isBeep = true;
                        mImBeep.setImageResource(R.mipmap.switch_on);
                    }
                    BACK_BEEP = true;
                    handler1.sendEmptyMessage(1115);
                    break;
                /** 自动关机 */
                case 3001:
                    int type1 = Integer.valueOf(mClose);
                    switch (type1) {
                        case 0:
                            mTvClose.setText(getString(R.string.setting_s0));
                            break;
                        case 1:
                            mTvClose.setText(getString(R.string.setting_s1));
                            break;
                        case 2:
                            mTvClose.setText(getString(R.string.setting_s2));
                            break;
                        case 3:
                            mTvClose.setText(getString(R.string.setting_s3));
                            break;
                    }
                    BACK_CLOSE = true;
                    handler1.sendEmptyMessage(1115);
                    break;
                /** 屏幕保护 */
                case 3002:
                    int type2 = Integer.valueOf(mScreen);
                    switch (type2) {
                        case 0:
                            mTvScreensave.setText(getString(R.string.setting_s0));
                            break;
                        case 1:
                            mTvScreensave.setText(getString(R.string.setting_s3));
                            break;
                        case 2:
                            mTvScreensave.setText(getString(R.string.min_1));
                            break;
                        case 3:
                            mTvScreensave.setText(getString(R.string.min_2));
                            break;
                    }
                    BACK_SCREEN = true;
                    handler1.sendEmptyMessage(1115);
                    break;
                /** Hz */
                case 3008:
                    int hz = Integer.valueOf(mHz);
                    switch (hz) {
                        case 0:
                            mTvHz.setText("50Hz");
                            break;
                        case 1:
                            mTvHz.setText("60Hz");
                            break;

                    }
                    BACK_HZ = true;
                    handler1.sendEmptyMessage(1115);
                    break;

                /** 曝光补偿 */
                case 3000:
                    int exposure = Integer.valueOf(mExposure);
                    switch (exposure) {
                        case 0:
                            mTvExposure.setText("-3");
                            break;
                        case 1:
                            mTvExposure.setText("-2");
                            break;
                        case 2:
                            mTvExposure.setText("-1");
                            break;
                        case 3:
                            mTvExposure.setText("0");
                            break;
                        case 4:
                            mTvExposure.setText("1");
                            break;
                        case 5:
                            mTvExposure.setText("2");
                            break;
                        case 6:
                            mTvExposure.setText("3");
                            break;

                    }
                    BACK_EXPOSURE = true;
                    handler1.sendEmptyMessage(1115);
                    break;
                /** 语言设置 */
                case 3006:
                    int language = Integer.valueOf(mLanguage);
                    switch (language) {
                        case 0:
                            mTvLanguage.setText(getString(R.string.language_cha));
                            break;
                        case 1:
                            mTvLanguage.setText(getString(R.string.language_chi));
                            break;
                        case 2:
                            mTvLanguage.setText(getString(R.string.language_eng));
                            break;
                        case 3:
                            mTvLanguage.setText(getString(R.string.language_deu));
                            break;
                        case 4:
                            mTvLanguage.setText(getString(R.string.language_jap));
                            break;

                    }
                    BACK_Language = true;
                    handler1.sendEmptyMessage(1115);
                    break;
                /** 重力感应 */
                case 2009:
                    int zl = Integer.valueOf(mHaver);
                    switch (zl) {
                        case 0:
                            mTvNormall.setText(getString(R.string.setting_s0));
                            break;
                        case 1:
                            mTvNormall.setText(getString(R.string.normall_1));
                            break;
                        case 2:
                            mTvNormall.setText(getString(R.string.normall_2));
                            break;
                        case 3:
                            mTvNormall.setText(getString(R.string.normall_3));
                            break;
                    }
                    BACK_HAVER = true;
                    handler1.sendEmptyMessage(1115);
                    break;
                /** 白平衡 */
                case 0022:
                    int bai = Integer.valueOf(mWhite);
                    switch (bai) {
                        case 0:
                            mTvWhite.setText(getString(R.string.white_1));
                            break;
                        case 1:
                            mTvWhite.setText(getString(R.string.white_2));
                            break;
                        case 2:
                            mTvWhite.setText(getString(R.string.white_3));
                            break;
                        case 3:
                            mTvWhite.setText(getString(R.string.white_4));
                            break;
                        case 4:
                            mTvWhite.setText(getString(R.string.white_5));
                            break;
                    }
                    BACK_WHITE = true;
                    handler1.sendEmptyMessage(1115);
                    break;
            }

        }
    };

    @Override
    public void onWiFiState(boolean isAutoDisable, int state) {
        super.onWiFiState(isAutoDisable, state);
        switch (state) {
            case 1:
                upData();
                //initSetting();
                upDataSetting(isUpdataSelect);
                //handler1.sendEmptyMessageDelayed(9999, 2000);
                isOnLine = true;
                mRlWaring.setVisibility(View.GONE);
                break;
            case 2:
                connectError();
                isOnLine = false;
                mRlWaring.setVisibility(View.VISIBLE);

                //没有连接设备wifi
                mTvName.setText(getString(R.string.err_mod));
                mTvPwd.setText(getString(R.string.err_mod));
                break;
        }
    }

    private void sendMessage() {
        isFirst = true;
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "2000");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "2008");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "3101");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "2002");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "2009");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "3010");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "3100");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "3001");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "3002");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "3008");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "3000");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, "3006");
        ControlManager.getInstance().requestStatus(CTP_ID_DEFAULT, CMD_WHITE_BALANCE);
    }

    private void upDataSetting(boolean isSelect) {

        if (!isSelect)
            return;

        isUpdataSelect = false;
        pb1 = ProgressDialog.show(SettingActivity.this, getString(R.string.dialog_tip), getString(R.string.loading_wait));
        sendMessage();
    }

    private void upData() {

        SharedPreferences sharedPreferences = PreferencesHelper.getSharedPreferences(MainApplication.getApplication());
        mOldName = sharedPreferences.getString(CURRENT_SSID, null);
        mOldPassword = sharedPreferences.getString(CURRENT_PWD, null);
        if (mApplication.getIsOffLineMode() == false) {
            //已经连接设备wifi
            if (null != mOldName) {
                //  mTvName.setText(mOldName.substring(WIFI_PREFIX.length()));
                mTvName.setText(mOldName);
            }
            if (!TextUtils.isEmpty(mOldPassword)) {
                mTvPwd.setText(mOldPassword);
            } else {
                mTvPwd.setText(getString(R.string.no_pwd));
            }
            mRlWaring.setVisibility(View.GONE);
        } else {
            //没有连接设备wifi
            mTvName.setText(getString(R.string.err_mod));
            mTvPwd.setText(getString(R.string.err_mod));
        }

    }

    /**
     * 恢复默认设置
     */
    private void showDefault() {
        twoDialog = new TwoDialog(SettingActivity.this, getString(R.string.dialog_tip), getString(R.string.is_contiune), defaultListener);
        twoDialog.show();
    }

    private View.OnClickListener defaultListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dialog_btn2_rl_ok:
                    twoDialog.dismiss();
                    /**Remove current Wi-Fi & the old SSID*/
                    SharedPreferences sharedPreferences = PreferencesHelper.getSharedPreferences(MainApplication.getApplication());
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    String currentSSID = sharedPreferences.getString(CURRENT_SSID, null);
                    editor.putString(currentSSID,null);
                    editor.remove(CURRENT_PWD);
                    //editor.putString(currentSSID,password);
                    editor.remove(CURRENT_SSID);
                    editor.apply();
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_RESET_DEVICE, ARGS_RESET_DEVICE);
                    isUpdataSelect = true;
                    break;
                case R.id.dialog_btn2_rl_canale:
                    twoDialog.dismiss();
                    break;
            }
        }
    };

    /**
     * 格式化
     */
    private void showFormat() {
        twoDialog = new TwoDialog(SettingActivity.this, getString(R.string.dialog_tip), getString(R.string.is_format), formatListener);
        twoDialog.show();
    }

    private View.OnClickListener formatListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dialog_btn2_rl_ok:
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "0010", "1");
                    pdFormat = ProgressDialog.show(SettingActivity.this, getString(R.string.dialog_tip), getString(R.string.formating));
                    twoDialog.dismiss();
                    break;
                case R.id.dialog_btn2_rl_canale:
                    twoDialog.dismiss();
                    break;
            }
        }
    };

    /**
     * 格式3
     */
    private void showThreeDialog(final boolean isSetting) {


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // final Dialog builder = new Dialog(getApplicationContext());
        View view = View.inflate(this, R.layout.dialog_btn2,
                null);
        TextView tvText = (TextView) view.findViewById(R.id.dialog_btn2_tv_info);
        RelativeLayout rlOk = (RelativeLayout) view.findViewById(R.id.dialog_btn2_rl_ok);
        RelativeLayout rlCancle = (RelativeLayout) view.findViewById(R.id.dialog_btn2_rl_canale);
        builder.setView(view);

        final AlertDialog installDialog = builder.create();
        installDialog.setCanceledOnTouchOutside(false);
        installDialog.setCancelable(false);
        installDialog.getWindow().setGravity(Gravity.CENTER);
        if (isSetting) {
            //恢复默认设置
            tvText.setText("确定后设备的所有参数将恢复默认设置,并且理赔保将要重启，是否继续？");
        } else {
            //格式化sd卡
            tvText.setText("格式化后将清除内存卡中所有内容,是否确认格式化?");
        }

        rlOk.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isSetting) {
                    //恢复默认设置
                    installDialog.dismiss();
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, CMD_RESET_DEVICE, ARGS_RESET_DEVICE);
                    //handler1.sendEmptyMessageDelayed(9998, 100);
                    isUpdataSelect = true;
                    //pb1 = ProgressDialog.show(SettingActivity.this, "提示", "正在关闭，请稍后……");
                } else {
                    //格式化sd卡
                    installDialog.dismiss();
                    ControlManager.getInstance().sendCommand(CTP_ID_DEFAULT, "0010", "1");
                    pdFormat = ProgressDialog.show(SettingActivity.this, getString(R.string.dialog_tip), "正在格式化，请稍后……");
                }


            }
        });

        rlCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                installDialog.dismiss();
            }
        });

        installDialog.setView(view, 0, 0, 0, 0);
        installDialog.show();
    }
}
